type Tdom_elements = null | Element | NodeListOf<Element>;

class KHTMLElement {
    get [Symbol.toStringTag]() { return KHTMLElement.name; };

    public elements: Array<HTMLElement> = [];

    public constructor(...elements: Array<Tdom_elements>) {
        elements.forEach(element => { this.push(element); });
    }

    private _push_(element: HTMLElement) {// Set 顺序不确定
        if (this.elements.indexOf(element) < 0) {
            this.elements.push(element);
        }
    }

    public push(element: Tdom_elements): void {
        if (element === null) {
        } else if (element instanceof HTMLElement) {
            this._push_(element);
        } else if (element instanceof NodeList) {
            element.forEach(e => { this.push(e); });
        } else {
            throw new IllegalArgumentError();
        }
    }

    public ele(i?: number): HTMLElement | null {
        if (typeof i === 'undefined') { i = 0; }
        if (i >= 0 && i < this.elements.length) {
            return this.elements[i];
        }
        return null;
    }

    public forEach(callbackfn: (value: HTMLElement, index: number, array: Array<HTMLElement>) => void) {
        this.elements.forEach(callbackfn);
    };

    public forEachK(callbackfn: (value: KHTMLElement, index: number, array: Array<HTMLElement>) => void) {
        this.elements.forEach(
            (value, index, array) => {
                callbackfn(new KHTMLElement(value), index, array);
            }
        );
    };
}
